# Based on linux-lg-mako. Changes:
# - add findutils makedepend (necessary for the initramfs)
# - it builds an initramfs - do not use ramdisk.cpio and
#	ramdisk-recovery-device.cpio, in there, always use the
#	initramfs from the recovery partition ("isorec")
# - directly boot to that initramfs, not only when the recovery
#	key combination was pressed.
# - use Alpine's busybox-static instead of the pre-compiled
#	binary, that comes with the source
#
# Kernel config changes, based on: arch/arm/configs/i9100_defconfig
# - enable devtmpfs (needed for udev -> touch support in weston)
# - change compression from CONFIG_KERNEL_LZMA=y to
#	CONFIG_KERNEL_GZIP=y (so it works with busybox)

_vendor=samsung
_flavor=samsung-i9100
_hash="349a3e91e76d17e67ef6213e1f6712e700695631"
_config="config-${_flavor}.armhf"

pkgname=linux-${_flavor}
pkgver=3.0.101
case $pkgver in
	*.*.*)  _kernver=${pkgver%.*};;
	*.*) _kernver=$pkgver;;
esac
pkgrel=9
arch="armhf"
pkgdesc="Samsung Galaxy SII kernel from LineageOS"
url="https://github.com/LineageOS/android_kernel_samsung_smdk4412"
depends="postmarketos-mkinitfs"
makedepends="perl sed installkernel bash gmp-dev bc linux-headers elfutils-dev
findutils busybox-static-armhf"
options="!strip !check !tracedeps"
install=
source="
	$pkgname-$_hash.tar.gz::https://github.com/LineageOS/android_kernel_samsung_smdk4412/archive/${_hash}.tar.gz
	$_config
	compiler-gcc6.h
	init
"
subpackages=""
license="GPL2"

_abi_release=${pkgver}
_carch="arm"
HOSTCC="${CC:-gcc}"
HOSTCC="${HOSTCC#${CROSS_COMPILE}}"

ksrcdir="$srcdir/android_kernel_samsung_smdk4412-${_hash}"

prepare() {
	local _patch_failed=
	cd "$ksrcdir"

	# first apply patches in specified order
	for i in $source; do
		case $i in
		*.patch)
			msg "Applying $i..."
			if ! patch -s -p1 -N -i "$srcdir"/$i; then
				echo $i >>failed
				_patch_failed=1
			fi
			;;
		esac
	done

	if ! [ -z "$_patch_failed" ]; then
		error "The following patches failed:"
		cat failed
		return 1
	fi

	# Use Alpine's busybox.static instead of the pre-compiled busybox shipped
	# with the source
	cp -v /usr/$(arch_to_hostspec $arch)/bin/busybox.static \
		"$ksrcdir"/usr/galaxys2_initramfs_files/busybox

	# do not use ramdisk.cpio and ramdisk-recovery-device.cpio, always use
	# the initramfs from the recovery partition ("isorec"), so we can build
	# it later and independently from the kernel. also directly boot that
	# partition, not only when the recovery key combination was used.
	cd "$ksrcdir"/usr/
	mv galaxys2_initramfs.list galaxys2_initramfs.list_old
	grep -v "../../ramdisk" galaxys2_initramfs.list_old > galaxys2_initramfs.list
	cp -v "$srcdir"/init "$ksrcdir"/usr/galaxys2_initramfs_files/init

	# gcc6 support
	cp -v "$srcdir/compiler-gcc6.h" "$ksrcdir/include/linux/"

	mkdir -p "$srcdir"/build
	cp "$srcdir"/$_config "$srcdir"/build/.config
	make -C "$ksrcdir" O="$srcdir"/build ARCH="$_carch" HOSTCC="$HOSTCC" \
		silentoldconfig
}



# this is so we can do: 'abuild menuconfig' to reconfigure kernel
menuconfig() {
	cd "$srcdir"/build
	make ARCH="$_carch" menuconfig
	cp .config "$startdir"/$_config
}

build() {
	cd "$srcdir"/build
	unset LDFLAGS

	make ARCH="$_carch" CC="${CC:-gcc}" \
		KBUILD_BUILD_VERSION="$((pkgrel + 1 ))-Alpine"
}

package() {
	install -Dm644 "$srcdir/build/arch/arm/boot/zImage" \
		"$pkgdir/boot/vmlinuz-$_flavor"

	install -D "$srcdir/build/include/config/kernel.release" \
		"$pkgdir/usr/share/kernel/$_flavor/kernel.release"
}

sha512sums="285ffd2c82d0444b99da583573d19443b73de22d7382aae54413af229a28bebf4ea32ecbe248ae54ae64bbbc0af7d722b1ef86d5dd099e2990dcc6284fb195fa  linux-samsung-i9100-349a3e91e76d17e67ef6213e1f6712e700695631.tar.gz
4e3901f5b8de0f97cc1e1e78b2f4e5fa639cbe6c236ae6ba8bd4e6080f2251fe5c1dc058b552d45cc5d34f608df5578e4a2655d9715ffeea9ff4c83bd977f09d  config-samsung-i9100.armhf
d80980e9474c82ba0ef1a6903b434d8bd1b092c40367ba543e72d2c119301c8b2d05265740e4104ca1ac5d15f6c4aa49e8776cb44264a9a28dc551e0d1850dcc  compiler-gcc6.h
dd4094d5f4ec281d32f12af88cb22a782e497c8e52f69cf60b73ac7d6171fc95f1f8040b3d0ad2ff3f016d22ac1d91c5b522e5d03203534a76742bc55a082af5  init"
